package fei.zhou.lambdalearn.demo6;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo8 {
    
    public static void main(String[] args) {
        // 创建一个集合，存储多个字符串元素
        ArrayList<String> list = new ArrayList<String>();
        
        list.add("林青霞");
        list.add("张曼玉");
        list.add("王祖贤");
        list.add("柳岩");
        list.add("张敏");
        list.add("张无忌");
        
        System.out.println("--------得到名字为3个字的流，把流操作完毕的数据收集到List集合中并遍历--------");
        Stream<String> stream1 = list.stream().filter(s1 -> s1.length() == 3);
        
        List<String> collect = stream1.collect(Collectors.toList());
        collect.forEach(System.out::println);
        
        // 定义一个字符串数组，每一个字符串数据由姓名数据和年龄数据组合而成
        String[] strArray = { "林青霞,30", "张曼玉,35", "王祖贤,33", "柳岩,25" };
        
        System.out.println("--------得到字符串中年龄数据大于28的流--------");
        Stream.of(strArray).filter(s1 -> Integer.parseInt(s1.split(",")[1]) > 28).forEach(System.out::println);
        
        System.out.println("--------得到字符串中年龄数据大于28的流,把流操作完毕的数据收集到Map集合中并遍历，字符串中的姓名作键，年龄作值--------");
        
        Stream<String> stream2 = Stream.of(strArray).filter(s1 -> Integer.parseInt(s1.split(",")[1]) > 28);
        
        Map<String, String> map = stream2.collect(Collectors.toMap(s1 -> s1.split(",")[0], s1 -> s1.split(",")[1]));
        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.println(key + "," + map.get(key));
        }
        
    }
    
}